package cn.edu.pet.servlet.admin;

import cn.edu.pet.entity.User;
import cn.edu.pet.repo.UserRepo;
import cn.edu.pet.util.PaginationUtil;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/admin/user/list")
public class UserListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        String page1 = req.getParameter("page");
        String name = req.getParameter("name");
        List<User> list = UserRepo.getInstance().selectList(name);
        PaginationUtil<User> page = new PaginationUtil<>();
        PrintWriter writer = resp.getWriter();
        String paginate = page.paginate(list, Integer.parseInt(page1));
        writer.println(paginate);
    }
}
